package com.ruicar.afs.cloud.apply.contract.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.apply.common.entity.GpsProductInfo;
import com.ruicar.afs.cloud.apply.contract.condition.GpsProductCondition;
import com.ruicar.afs.cloud.apply.contract.service.GpsProductInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author panhj
 */
@Slf4j
@RestController
@RequestMapping("/gpsProduct")
@AllArgsConstructor
public class GpsProductController {
    private final GpsProductInfoService gpsProductInfoService;

    @PostMapping("/productInfo")
    @ApiOperation(value = "获取GPS产品信息")
    public IResponse contractEntryInfo(@RequestBody GpsProductCondition condition) {
        Page page = new Page(condition.getPageNumber(), condition.getPageSize());
        //供应商 期限
        IPage<GpsProductInfo> list=gpsProductInfoService.page(page,Wrappers.<GpsProductInfo>query().lambda()
        .eq(StringUtils.isNotEmpty(condition.getGpsFirm()),GpsProductInfo::getGpsFirm,condition.getGpsFirm())
        .eq(StringUtils.isNotEmpty(condition.getGpsType()),GpsProductInfo::getGpsType,condition.getGpsType())
        .eq(condition.getDownTerm()!=null,GpsProductInfo::getDownTerm, condition.getDownTerm())
        .eq(condition.getUpTerm()!=null,GpsProductInfo::getUpTerm, condition.getUpTerm()));
        return IResponse.success(list);
    }

    @PostMapping("/addOrUpdateProduct")
    @ApiOperation(value = "添加或修改GPS产品")
    public IResponse addOrUpdateProduct(@RequestBody GpsProductCondition condition){
        if(condition.getId()==null){
            gpsProductInfoService.save(condition);
        }else{
            GpsProductInfo gpsProductInfo=gpsProductInfoService.getById(condition.getId());
            BeanUtils.copyProperties(condition, gpsProductInfo);
            gpsProductInfoService.updateById(gpsProductInfo);
        }
        return IResponse.success("操作成功").setData(condition.getId().toString());
    }

    @PostMapping("/removeProduct")
    @ApiOperation(value = "删除GPS产品")
    public IResponse removeProduct(@RequestBody GpsProductCondition condition){
        gpsProductInfoService.removeById(condition.getId());
        return IResponse.success("删除成功");
    }
}
